home *** CD-ROM | disk | FTP | other *** search
/ Enter 2005 October / enter-2005-10.iso / files / jedit42install.exe / {app} / macros / Java / Get_Class_Name.bsh < prev    next >
Encoding:
Text File  |  2004-08-29  |  2.8 KB  |  93 lines

  1. /**
  2. Get_Class_Name.bsh - a BeanShell macro for the jEdit text editor replaces the 
  3. selected text with the current class name.
  4.  
  5. This macro walks though several steps in an attempt to guess the correct class
  6. name.
  7.  
  8. First, it searches backwards for the nearest occurance of the keyword 'class,'
  9. so it should work for inner class definitions.  It does not, however, do any 
  10. fancy code parsing, so if you run this macro at any point after an inner class 
  11. definition, it will return the name of the inner class, not the parent class.
  12.  
  13. If that fails, it attempts to guess the class name from the file name; i.e. it 
  14. would return "FooBar" if the buffer was named "FooBar.cc."  If this fails (if, 
  15. for example, the buffer is untitled), it returns an empty string.
  16.  
  17. Copyright (C)  2004 Thomas Galvin - software@thomas-galvin.com
  18.  
  19. This program is free software; you can redistribute it and/or
  20. modify it under the terms of the GNU General Public License
  21. as published by the Free Software Foundation; either version 2
  22. of the License, or any later version.
  23.  
  24. This program is distributed in the hope that it will be useful,
  25. but WITHOUT ANY WARRANTY; without even the implied warranty of
  26. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  27. GNU General Public License for more details.
  28. */
  29.  
  30. void setCaret(int selectionStart, int selectionEnd)
  31. {
  32.   textArea.setCaretPosition(selectionStart);
  33.   textArea.moveCaretPosition(selectionEnd);
  34. }
  35.  
  36. String getClassName()
  37. {
  38.   int selectionStart = textArea.getSelectionStart();
  39.   int selectionEnd = textArea.getSelectionEnd();
  40.   
  41.   String text = textArea.getText();
  42.   int index = text.lastIndexOf("class", selectionStart);
  43.   if(index != -1)
  44.   {
  45.     textArea.setCaretPosition(index);
  46.     int lineNumber = textArea.getCaretLine();
  47.     int lineEnd = textArea.getLineEndOffset(lineNumber);
  48.     String lineText = text.substring(index, lineEnd);
  49.     
  50.     StringTokenizer tokenizer = new StringTokenizer(lineText);
  51.     tokenizer.nextToken(); //eat "class"
  52.     if(tokenizer.hasMoreTokens())
  53.     {
  54.       setCaret(selectionStart, selectionEnd);
  55.       return tokenizer.nextToken();
  56.     }
  57.   }
  58.   
  59.   
  60.   String fileClassName = buffer.getName();
  61.   int index = fileClassName.lastIndexOf('.');
  62.   if(index != -1)
  63.   {
  64.     fileClassName = fileClassName.substring(0, index);
  65.     if(fileClassName.toLowerCase().indexOf("untitled") == -1)
  66.     {
  67.       return fileClassName;
  68.     }
  69.   }
  70.   setCaret(selectionStart, selectionEnd);
  71.   
  72.   String className = buffer.getName();
  73.   int index = name.lastIndexOf('.');
  74.   if(index != -1)
  75.   {
  76.     className = className.substring(0, index);
  77.     if(className.toLowerCase().indexOf("untitled") == -1)
  78.     {
  79.       return className;
  80.     }
  81.   }
  82.   
  83.   return "";
  84. }
  85.  
  86. String className = getClassName();
  87. if( buffer.isReadOnly() )
  88.     Macros.error( view, "Buffer is read-only." );
  89. else if( className != null && !className.equals("") )
  90. {
  91.   textArea.setSelectedText(className);
  92. }
  93.